import globalDictionary, { IGlobalDictionary } from '../globalStore/dictionary';
import { DictItem, WordsType } from '../typing';

export function findMatchedDictItems(type: 'all' | WordsType, keyword: string) {
  const matchedDictItems: DictItem[] = [];

  if (type === 'all' || type === 'words') {
    matchedDictItems.push(...findMatchedDictItemsByMapType('dictWordMap', keyword));
  }

  if (type === 'all' || type === 'translatedWords')  {
    matchedDictItems.push(...findMatchedDictItemsByMapType('dictTranslatedWordMap', keyword));
  }

  if (type === 'all' || type === 'synonyms')  {
    matchedDictItems.push(...findMatchedDictItemsByMapType('dictSynonymMap', keyword));
  }

  return matchedDictItems;
}

function findMatchedDictItemsByMapType(type: 'dictWordMap' | 'dictTranslatedWordMap' | 'dictSynonymMap', keyword: string) {
  const dictItem = globalDictionary[type][keyword];

  return dictItem
    ? Array.isArray(dictItem)
      ? dictItem
      : [dictItem]
    : [];
}